package com.htfyun.factorytest.factorytestui;

import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import com.htfyun.factorytest.R;

/**
 * <pre>
 *     author : Administrator
 *     e-mail : tancheng@htfyun.com
 *     time   : 2020/09/10
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class FactoryCameraActivity extends FactoryBaseActivity {
    private Camera mCamera;
    private CameraPreview mPreview;

    @Override
    int getLayoutId() {
        return R.layout.activity_factory_camera;
    }

    @Override
    void onInitView(@Nullable Bundle savedInstanceState) {
        // Create an instance of Camera
        mCamera = Camera.open();

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = findViewById(R.id.camera_preview);
        preview.addView(mPreview);
    }

    @Override
    void onHandleMessage(Handler handler, Message msg) {

    }

    @Override
    protected void onPause() {
        super.onPause();
        stopPreviewAndFreeCamera();
    }

    private void stopPreviewAndFreeCamera() {

        if (mCamera != null) {
            // Call stopPreview() to stop updating the preview surface.
            mCamera.stopPreview();

            // Important: Call release() to release the camera for use by other
            // applications. Applications should release the camera immediately
            // during onPause() and re-open() it during onResume()).
            mCamera.release();

            mCamera = null;
        }
    }

}
